/*
 * Copyright 2007 The semap project development team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Created by myn at 21.08.2007 17:21:50.
 *
 * This class is a handler of the ADD command.
 *
 * $Id: AddCmdProc.java 434 2007-08-24 12:00:45Z Mike.Navern $
 */
package com.google.code.contactserv;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import com.google.code.contactserv.model.Contact;
import com.google.code.contactserv.model.PropertyHolder;
import com.google.code.contactserv.service.ContactService;
import com.hp.hpl.jena.rdf.model.Property;

/**
 * @author myn
 *
 */
public class AddCmdProc extends CommandProcessor {

    /**
     * @param srv
     * @param line
     */
    public AddCmdProc(ContactService srv, List<PropertyHolder> lst) {
        super(srv, lst);
    }

    /* 
     * 
     */
    @Override
    public void printIntroduction() {
        System.out.println("Welcome to contactserv version 1.0.");
        System.out.println("Insert a contact URI to process it or press 'ENTER' (an empty URI) to finish.");
    }

    public String prepareString(String str) {
        String s = str.trim();
        if ((s.charAt(0) == '\'') || (s.charAt(0) == '"')) {
            if (s.length() < 3) {
                s = "";
            } else {
                s = s.substring(1, s.length() - 1);
            }
        }
        return s;
    }
    
    /**
     * @param contact - contact to be modified 
     * @param prop    - contact property to be modified 
     * @param str     - list of values, separated by comma
     */
    public void setupProperties(Contact contact, Property prop, String str) {
        String[] lst = str.split(",");
        for (int i = 0; i < lst.length; i++) {
            String[] pair = lst[i].split("@");
            String   s = "";
            if (pair.length == 1) {
                s = prepareString(pair[0]);
                contact.setProp(prop, s);
            } else {        // pair.length >= 2         we will ignore subsequent values  
                s = prepareString(pair[0]);
                contact.setProp(prop, s, pair[1].trim());
            }
        }
    }

    /* (non-Javadoc)
     * @see com.google.code.contactserv.CommandProcessor#doWork(java.lang.String)
     */
    @Override
    public void doWork(String uri) {
        Contact contact = srv.createContact(uri);
        
        try {
            for (Iterator<PropertyHolder> i = lst.iterator(); i.hasNext();) {
                PropertyHolder holder = (PropertyHolder) i.next();
                System.out.print(holder.getName() + ": ");
                String str = readNext();
                setupProperties(contact, holder.getProperty(), str);
            }
            srv.add(contact);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
